
/**
\file
Defines the rainbow::depth_state class.
\author Sean Tromans
*/

#include <sun_pre_compiled.hpp>
#include <rainbow/graphics_device/depth_state/depth_state.hpp>

#if !defined(SUN_DISABLE_LUA)
#include <sun/lua/lua.hpp>
#endif

namespace rainbow
{
	/**
	Default Constructor.
	\author Sean Tromans
	*/
	depth_state::depth_state( )
		:		test_enabled(true)
			,	write_enabled(true)

			,	depth_func(GL_LESS)
			,	depth_near(0.0)
			,	depth_far(1.0)
	{}

	/**
	Applies any changes from the provided depth_state to OpenGL.
	@param other The currently active blend_state.
	\author Sean Tromans
	*/
	void depth_state::apply( depth_state const & other ) const
	{
		if ( test_enabled != other.test_enabled )
		{
			sun::detail::set_state( GL_DEPTH_TEST, test_enabled );
		}

		if ( write_enabled != other.write_enabled )
		{
			glDepthMask( write_enabled ? GL_TRUE : GL_FALSE );
		}

		if ( depth_func != other.depth_func )
		{
			glDepthFunc( depth_func );
		}

		if (	depth_near != other.depth_near
			||	depth_far != other.depth_far )
		{
			glDepthRange( depth_near, depth_far );
		}
	}

	/**
	Exposes the depth_state to lua.
	@param L The Lua State being initialised.
	\author Sean Tromans.
	*/
	void depth_state::lua_interface( lua_State * L )
	{
#if !defined(SUN_DISABLE_LUA)
		luabind::module( L, "rainbow" )
		[
			luabind::class_< depth_state >( "depth_state" )
				.def( luabind::constructor<>( ) )
				.def_readwrite( "test_enabled", &depth_state::test_enabled )
				.def_readwrite( "write_enabled", &depth_state::write_enabled )
				.def_readwrite( "depth_func", &depth_state::depth_func )
				.def_readwrite( "depth_near", &depth_state::depth_near )
				.def_readwrite( "depth_far", &depth_state::depth_far )
		];
#else
		NMN_UNUSED( L );
#endif
	}

	/**
	Comparrison operator for depth_states.
	@param lhs The first depth_state being compared.
	@param rhs The second depth_state being compared.
	@return True if the depth_states are equal.
	\author Sean Tromans
	*/
	bool operator == ( depth_state const & lhs, depth_state const & rhs )
	{
		return	lhs.test_enabled == rhs.test_enabled
			&&	lhs.write_enabled == rhs.write_enabled
			&&	lhs.depth_func == rhs.depth_func
			&&	lhs.depth_near == rhs.depth_near
			&&	lhs.depth_far == rhs.depth_far;
	}

}
